package com.mall.service.impl;

import com.mall.dao.UserMapper;
import com.mall.entity.User;
import com.mall.service.UserService;
import com.mall.utils.MD5Utils;
import com.mall.utils.StringUtils;
import com.mall.vo.ResultVO;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

@Service
@Scope("singleton")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public ResultVO userRegist(User user) {
        User user2 = userMapper.queryUserByName(user.getUsername());
        if (user2 ==null) {
            synchronized (this) {
                if (user2 == null) {
                    String md5Pwd = MD5Utils.md5(user.getPassword());
                  //  user2.setUsername(user.getUsername());
                    user.setPassword(md5Pwd);
                    user.setUserImg("img/default.png");
                    Date date = new Date();
                    user.setUserRegtime(date);
                    user.setUserModtime(date);
                    Integer i = userMapper.insertUser(user);
                    if (i > 0) {
                        return ResultVO.success("用户注册成功！");
                    } else {
                        return ResultVO.fail("注册失败，请稍后重试！");
                    }
                }else {
                    return ResultVO.fail("用户名已存在！");
                }
            }
        }else {
            return ResultVO.fail("用户名已存在！");
        }
    }

    @Override
    public ResultVO userLogin(User user) {
        // 校验请求参数
        if (StringUtils.isBlank(user.getUsername())||StringUtils.isBlank(user.getPassword())){
            return ResultVO.fail("账号，密码不能为空");
        }
        //执行业务
        User user1 = userMapper.queryUserByName(user.getUsername());
        if (StringUtils.isBlank(user1)){
            return ResultVO.fail("用户名或密码错误!");
        }else {
        String md5Pwd=MD5Utils.md5(user.getPassword());
        if (md5Pwd.equals(user1.getPassword())){
            //如果登录验证通过，则需要生成令牌token
            //String token = Base64Utils.encode(user.getUsername() + "mall");
            //使用jwt生成token
            HashMap<String, Object> map = new HashMap<>();
            map.put("user",user1);
            JwtBuilder builder = Jwts.builder();
            String token = builder.setIssuedAt(new Date())  //设置token的生成时间
                    .setId(user1.getUserId() + "") //设置用户id为token的id
                    .setClaims(map)           //map可以存放用户的角色权限信息
                    .setExpiration(new Date(System.currentTimeMillis() + 72 * 60 * 60 * 1000)) //token过期时间是3天
                    .signWith(SignatureAlgorithm.HS256, "mall666")
                    .compact();
            return ResultVO.success(token,user1);
           }else {
            return ResultVO.fail("用户名或密码错误!");
          }

        }
    }
}
